package com.videos.controller;

import com.alibaba.fastjson.JSON;
import com.commons.annotations.RequiredToken;
import com.commons.entity.MovieUserInfo;
import com.commons.enums.ResponseCodeEnum;
import com.commons.po.MovieInfoPo;
import com.commons.utils.AutoGenerateIds;
import com.commons.utils.Constance;
import com.commons.utils.ResponseResult;
import com.commons.vo.QueryMovieInfoVo;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.videos.entity.HistoryMoviePo;
import com.videos.service.MovieService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mis_wu
 * @date 2022/2/27 17:06
 * 电影信息控制器
 */
@RestController
@RequestMapping("/videos-services")
@Api(tags = "Movies-Service-Api 视频微服务接口")
@SuppressWarnings("unchecked")
@Slf4j
@RefreshScope
public class MovieController {

    @Autowired
    private MovieService movieService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${logging.file.name}")
    private String logName;
    @Value("${spring.datasource.password}")
    private String dataPwd;
    @Value("${spring.datasource.url}")
    private String dataUrl;

    @GetMapping("/getLogName")
    public String getLogName() {
        return "日志名:"+logName+",数据库密码："+dataPwd+"，url:"+dataUrl;
    }

    /**
     * vip和普通电影全部展示 登录之后
     */
    @GetMapping("/queryMovieInfoByPage")
    @ApiOperationSupport(author = "wubo")
    @ApiOperation("已登录分页查询电影信息")
    @RequiredToken
    public ResponseResult<Map> queryMovieInfoByPage(QueryMovieInfoVo queryMovieInfoVo){
        Map<String, Object> map=new HashMap<>(5);
        PageInfo<MovieInfoPo> pageInfo = movieService.queryMovieInfoByPage(queryMovieInfoVo);
        map.put("movieList",pageInfo.getList());
        map.put("totals",pageInfo.getTotal());
        return new ResponseResult<>(0, "success", map);
    }

    /**
     * 游客登录查询
     */
    @GetMapping("/queryCommonsMovies")
    @ApiOperation("游客登录展示电影")
    @RequiredToken
    public ResponseResult queryCommonsMovies(@ApiParam("关键字搜索") QueryMovieInfoVo queryMovieInfoVo){
        List<MovieInfoPo> movieList = movieService.queryCommonsMovies(queryMovieInfoVo);
        Map map = new HashMap<>(5);
        if (movieList.isEmpty() || movieList==null){
            return new ResponseResult(9999,"没有匹配到类似的电影！！！");
        }
        map.put("movieList",movieList);
        map.put("totals",movieList.size());
        return new ResponseResult<>(ResponseCodeEnum.SUCCESS.getCode(),ResponseCodeEnum.SUCCESS.getMsg(),map);
    }

    /**
     * 查询历史搜索记录
     * @param request
     * @return
     */
    @GetMapping("/queryHistoryMovie")
    @RequiredToken
    @ApiOperation("查询历史搜索记录")
    public ResponseResult<HistoryMoviePo> queryHistoryMovie(HttpServletRequest request){
        //在拦截器中将token setAttribute
        String userInfo =stringRedisTemplate.opsForValue().get(Constance.TOKEN_KEY+request.getAttribute("token"));
        MovieUserInfo movieUserInfo= JSON.parseObject(userInfo,MovieUserInfo.class);
        if (ObjectUtils.isEmpty(movieUserInfo)){
            return new ResponseResult(ResponseCodeEnum.LOGIN_INFO_ISNULL.getCode(),ResponseCodeEnum.LOGIN_INFO_ISNULL.getMsg());
        }
        List<HistoryMoviePo> historyMoviePosList = movieService.queryHistoryMovie(movieUserInfo.getUserId());
        return new ResponseResult(ResponseCodeEnum.SUCCESS.getCode(),ResponseCodeEnum.SUCCESS.getMsg(),historyMoviePosList);
    }

    @GetMapping("/queryMoviesBySearchKey/{page}/{size}")
    @ApiOperation("根据关键字搜索")
    @RequiredToken
    public ResponseResult<Map> queryMoviesBySearchKey(@PathVariable("page") int page,@PathVariable("size") int size,@RequestParam String searchKey){
        Map map = new HashMap(5);
        PageInfo<MovieInfoPo> moviePageInfo = movieService.queryMoviesBySearchKey(page,size,searchKey);
        map.put("searchList",moviePageInfo.getList());
        map.put("totals",moviePageInfo.getTotal());
        return new ResponseResult(ResponseCodeEnum.SUCCESS.getCode(),ResponseCodeEnum.SUCCESS.getMsg(),map);
    }

    @ApiOperation("测试controller")
    @GetMapping("/test")
    @RequiredToken
    public String test(){
        redisTemplate.opsForValue().set("test","hello test!!!");
        return "hello world!!!";
    }

    public static void main(String[] args) {
        List<Long> id = AutoGenerateIds.getGenerateId(1);
        log.info("id:{}",id.get(0));
    }
}
